// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Photos.Domain.Entities;

/// <summary>
/// AI照片分析实体
/// 支持各种AI分析和识别功能
/// </summary>
public class AIPhotoAnalysis : FullAuditedEntity<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public virtual Guid? TenantId { get; protected set; }

    /// <summary>
    /// 照片ID
    /// </summary>
    public virtual Guid PhotoId { get; protected set; }

    /// <summary>
    /// 分析类型
    /// </summary>
    public virtual AnalysisType AnalysisType { get; protected set; }

    /// <summary>
    /// 分析状态
    /// </summary>
    public virtual AnalysisStatus Status { get; protected set; }

    /// <summary>
    /// 分析引擎
    /// </summary>
    [Required]
    [MaxLength(100)]
    public virtual string Engine { get; protected set; } = string.Empty;

    /// <summary>
    /// 引擎版本
    /// </summary>
    [MaxLength(50)]
    public virtual string? EngineVersion { get; protected set; }

    /// <summary>
    /// 分析模型
    /// </summary>
    [MaxLength(100)]
    public virtual string? Model { get; protected set; }

    /// <summary>
    /// 分析参数（JSON格式）
    /// </summary>
    public virtual string? Parameters { get; protected set; }

    /// <summary>
    /// 分析结果（JSON格式）
    /// </summary>
    public virtual string? Result { get; protected set; }

    /// <summary>
    /// 置信度
    /// </summary>
    public virtual double Confidence { get; protected set; }

    /// <summary>
    /// 处理时长（毫秒）
    /// </summary>
    public virtual long ProcessingTimeMs { get; protected set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    [MaxLength(1000)]
    public virtual string? ErrorMessage { get; protected set; }

    /// <summary>
    /// 分析时间
    /// </summary>
    public virtual DateTime AnalyzedAt { get; protected set; }

    /// <summary>
    /// 关联照片
    /// </summary>
    public virtual Photo Photo { get; protected set; } = null!;

    #region 构造函数

    /// <summary>
    /// 参数化构造函数
    /// </summary>
    /// <param name="id">分析ID</param>
    /// <param name="tenantId">租户ID</param>
    /// <param name="photoId">照片ID</param>
    /// <param name="analysisType">分析类型</param>
    /// <param name="engine">分析引擎</param>
    public AIPhotoAnalysis(
        Guid id,
        Guid? tenantId,
        Guid photoId,
        AnalysisType analysisType,
        string engine) : base(id)
    {
        TenantId = tenantId;
        PhotoId = photoId;
        AnalysisType = analysisType;
        Engine = engine;
        Status = AnalysisStatus.Pending;
        AnalyzedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// 无参构造函数（用于EF Core）
    /// </summary>
    protected AIPhotoAnalysis()
    {
    }

    #endregion

    #region 业务方法

    /// <summary>
    /// 开始分析
    /// </summary>
    /// <param name="engineVersion">引擎版本</param>
    /// <param name="model">模型</param>
    /// <param name="parameters">参数</param>
    public virtual void StartAnalysis(string? engineVersion = null, string? model = null, string? parameters = null)
    {
        if (Status != AnalysisStatus.Pending)
        {
            throw new InvalidOperationException("只能对待处理状态的分析开始处理");
        }

        Status = AnalysisStatus.Processing;
        EngineVersion = engineVersion;
        Model = model;
        Parameters = parameters;
    }

    /// <summary>
    /// 完成分析
    /// </summary>
    /// <param name="result">分析结果</param>
    /// <param name="confidence">置信度</param>
    /// <param name="processingTimeMs">处理时长</param>
    public virtual void CompleteAnalysis(string result, double confidence, long processingTimeMs)
    {
        if (Status != AnalysisStatus.Processing)
        {
            throw new InvalidOperationException("只能对处理中状态的分析完成处理");
        }

        Status = AnalysisStatus.Completed;
        Result = result;
        Confidence = Math.Clamp(confidence, 0.0, 1.0);
        ProcessingTimeMs = processingTimeMs;
        AnalyzedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// 分析失败
    /// </summary>
    /// <param name="errorMessage">错误信息</param>
    public virtual void FailAnalysis(string errorMessage)
    {
        if (Status != AnalysisStatus.Processing)
        {
            throw new InvalidOperationException("只能对处理中状态的分析设置失败");
        }

        Status = AnalysisStatus.Failed;
        ErrorMessage = errorMessage;
        AnalyzedAt = DateTime.UtcNow;
    }

    #endregion
}

/// <summary>
/// 分析类型枚举
/// </summary>
public enum AnalysisType
{
    /// <summary>
    /// 人脸识别
    /// </summary>
    FaceRecognition = 1,

    /// <summary>
    /// 场景分类
    /// </summary>
    SceneClassification = 2,

    /// <summary>
    /// 物体检测
    /// </summary>
    ObjectDetection = 3,

    /// <summary>
    /// 文字识别（OCR）
    /// </summary>
    TextRecognition = 4,

    /// <summary>
    /// 情感分析
    /// </summary>
    EmotionRecognition = 5,

    /// <summary>
    /// 图像质量评估
    /// </summary>
    QualityAssessment = 6,

    /// <summary>
    /// 相似度匹配
    /// </summary>
    SimilarityMatching = 7,

    /// <summary>
    /// 风格分析
    /// </summary>
    StyleAnalysis = 8,

    /// <summary>
    /// 色彩分析
    /// </summary>
    ColorAnalysis = 9,

    /// <summary>
    /// 自定义分析
    /// </summary>
    Custom = 99
}

/// <summary>
/// 分析状态枚举
/// </summary>
public enum AnalysisStatus
{
    /// <summary>
    /// 待处理
    /// </summary>
    Pending = 0,

    /// <summary>
    /// 处理中
    /// </summary>
    Processing = 1,

    /// <summary>
    /// 已完成
    /// </summary>
    Completed = 2,

    /// <summary>
    /// 失败
    /// </summary>
    Failed = 3,

    /// <summary>
    /// 已取消
    /// </summary>
    Cancelled = 4
}